package cn.zyjblogs.starter.sms.provider.tencent;

import cn.zyjblogs.starter.sms.common.SmsCommon;
import cn.zyjblogs.starter.sms.common.SmsEnum;
import cn.zyjblogs.starter.sms.common.SmsRequest;
import cn.zyjblogs.starter.sms.common.SmsResponse;
import cn.zyjblogs.starter.sms.config.SmsProperties;
import cn.zyjblogs.starter.sms.exception.SmsRuntimeException;
import cn.zyjblogs.starter.sms.provider.AbstractSmsGranter;
import cn.zyjblogs.starter.sms.provider.SmsGranter;
import com.alibaba.fastjson2.JSON;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author zhuyijun
 */
public class TenSmsHandleGranter extends AbstractSmsGranter {
    private static final SmsEnum TYPE_NAME = SmsEnum.TEN_SMS;
    private final SmsClient smsClient;
    private final SmsProperties.TencentSmsProperties tencentSmsProperties;

    public TenSmsHandleGranter(SmsClient smsClient, SmsProperties.TencentSmsProperties tencentSmsProperties, SmsEnum typeName) {
        super(typeName);
        this.smsClient = smsClient;
        this.tencentSmsProperties = tencentSmsProperties;
        if (tencentSmsProperties.getMaxSize() == null || tencentSmsProperties.getMaxSize() < 100) {
            tencentSmsProperties.setMaxSize(200);
        }
    }

    public TenSmsHandleGranter(SmsClient smsClient, SmsProperties.TencentSmsProperties tencentSmsProperties) {
        super(TYPE_NAME);
        this.smsClient = smsClient;
        this.tencentSmsProperties = tencentSmsProperties;
    }

    @Override
    public SmsResponse sendSms(List<String> phones, SmsRequest smsRequest) {
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        sendSmsRequest.setPhoneNumberSet(phones.toArray(new String[0]));
        return getSmsResponse(smsRequest, sendSmsRequest);
    }

    private SmsResponse getSmsResponse(SmsRequest smsRequest, SendSmsRequest sendSmsRequest) {
        sendSmsRequest.setSmsSdkAppid(tencentSmsProperties.getAppId());
        sendSmsRequest.setSign(tencentSmsProperties.getSign());
        if (!StringUtils.hasLength(smsRequest.getTemplateCode())) {
            throw new SmsRuntimeException("模板id不能为空");
        }
        Map<String, Object> paramsMap = smsRequest.getParamsMap();
        Object template = paramsMap.get(SmsCommon.TEMPLATE_PARAM);
        if (template == null) {
            throw new SmsRuntimeException("参数不能为空");
        }
        List<String> lists = JSON.parseArray(JSON.toJSONString(template), String.class);
        sendSmsRequest.setTemplateParamSet(lists.toArray(new String[0]));
        if (paramsMap.containsKey(SmsCommon.SENDERID)) {
            sendSmsRequest.setSenderId(paramsMap.get(SmsCommon.SENDERID).toString());
        }
        try {
            SendSmsResponse sendSmsResponse = smsClient.SendSms(sendSmsRequest);
            return SmsResponse.builder()
                    .code("OK")
                    .message("发送成功")
                    .data(sendSmsResponse)
                    .build();
        } catch (TencentCloudSDKException e) {
            throw new SmsRuntimeException("发送失败");
        }
    }

    @Override
    public SmsResponse sendSms(String phone, SmsRequest smsRequest) {
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        sendSmsRequest.setPhoneNumberSet(new String[]{phone});
        return getSmsResponse(smsRequest, sendSmsRequest);
    }

    @Override
    public SmsGranter getGranter(SmsEnum typeName) {
        if (TYPE_NAME.equals(typeName)) {
            return this;
        }
        return null;
    }
}
